package com.duanxian.core.db;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by llx on 2017/8/12.
 * @author Min
 */
@Repository("sqlQuery")
public class DbTemplateImpl implements DbTemplate {

    @Autowired
    private DataSource dataSource;

    private static ThreadLocal<Connection> connection = new ThreadLocal<>();

    /**
     * 获取当前线程的连接
     * @return
     */
    private Connection getConnection(){
        synchronized (this){
            if(connection.get()==null){
                try {
                    connection.set(dataSource.getConnection());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return connection.get();
    }

    /**
     * 用完移除连接
     */
    private void removeConnetion(){
        connection.remove();
    }

    /**
     * 查询
     * @param sql
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> query(String sql, List<Object> params) {
        QueryRunner queryRunner = new QueryRunner(dataSource);
        List<Map<String, Object>> result = null;
        try {
            Object[] arrays = new Object[]{};
            if(params!=null){
                arrays=params.toArray();
            }
            result = queryRunner.query(sql, new MapListHandler(), arrays);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询返回一条记录
     * @param sql
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryOne(String sql, List<Object> params) {
        QueryRunner queryRunner = new QueryRunner(dataSource);
        Map<String, Object> result = null;
        try {
            Object[] arrays = new Object[]{};
            if(params!=null){
                arrays=params.toArray();
            }
            result = queryRunner.query(sql, new MapHandler(), arrays);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新
     * @param sql
     * @param params
     * @return
     */
    @Override
    public int update(String sql, List<Object> params) {
        QueryRunner queryRunner = new QueryRunner();
        int result = 0;
        try {
            Object[] arrays = new Object[]{};
            if(params!=null){
                arrays=params.toArray();
            }
            result = queryRunner.update(getConnection(),sql,arrays);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            removeConnetion();
        }
        return result;
    }

    /**
     * 删除
     * @param sql
     * @param params
     * @return
     */
    @Override
    public int delete(String sql, List<Object> params) {
        return this.update(sql,params);
    }
}
